package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	log "github.com/micro/go-micro/v2/logger"
	"web-service/api/entity"
	"web-service/api/middleware"
	"go-library/structutils"
	"go-library/validate"
	"rpc-service/product/proto/product"
	"strconv"
)

type ProductController struct {
	BaseController
}

/**
 * 获取查询列表
 */
func (ctrl *ProductController) GetProductList(ctx *gin.Context) {

	//GET方式获取query string
	sizeStr := ctx.DefaultQuery("size", "10")
	size, _ := strconv.Atoi(sizeStr)
	//大写的原因,需要把小写的转义一下
	var prodReq product.ProdRequest
	prodReq.Size = int32(size)

	prodRpcClient := middleware.GetProductRpcClient()
	prodResp, _ := prodRpcClient.GetProductList(context.Background(), &prodReq)

	log.Info("prodService.GetProdList:", prodResp)
	ctrl.ResponseData(ctx, prodResp.Data)
}

/**
 * 获取详情
 */
func (ctrl *ProductController) GetProductDetail(ctx *gin.Context) {

	sizeStr := ctx.DefaultQuery("id", "0")
	size, _ := strconv.Atoi(sizeStr)
	//大写的原因,需要把小写的转义一下
	var prodReq product.Product
	prodReq.ProdId = int32(size)

	prodRpcClient := middleware.GetProductRpcClient()
	prodResp, _ := prodRpcClient.GetProductDetail(context.Background(), &prodReq)
	log.Info("prodService.GetProductDetail:", prodResp)
	ctrl.Response(ctx, prodResp.Code, prodResp.Data)
}

/**
 * 创建商品
 */
func (ctrl *ProductController) CreateProduct(ctx *gin.Context) {
	//声明一个接收参数的实体
	var prodEntity entity.Product
	//绑定参数，并验证合法性
	err := validate.Check(ctx, &prodEntity)
	if err != nil {
		//如果不合法，响应错误码和信息
		ctrl.ResponseFailed(ctx, 4000, err.Error())
		return
	}

	var prodReq product.Product
	structutils.CopyProperties(&prodEntity, &prodReq)

	prodRpcClient := middleware.GetProductRpcClient()
	prodResp, _ := prodRpcClient.CreateProduct(context.Background(), &prodReq)
	log.Info("prodService.CreateProduct:", prodResp)
	ctrl.Response(ctx, prodResp.Code, prodResp.Data)
}

/**
 * 更新商品
 */
func (ctrl *ProductController) UpdateProduct(ctx *gin.Context) {
	//声明一个接收参数的实体
	var prodEntity entity.Product
	//绑定参数，并验证合法性
	err := validate.Check(ctx, &prodEntity)
	if err != nil {
		//如果不合法，响应错误码和信息
		ctrl.ResponseFailed(ctx, 4000, err.Error())
		return
	}

	//更新时必须要传
	if prodEntity.ProdId <= 0 {
		ctrl.ResponseFailed(ctx, 4000, "prodId参数不能为空")
		return
	}

	//grpc调用
	var prodReq product.Product
	structutils.CopyProperties(&prodEntity, &prodReq)
	prodRpcClient := middleware.GetProductRpcClient()
	prodResp, _ := prodRpcClient.UpdateProduct(context.Background(), &prodReq)
	log.Info("prodService.UpdateProduct:", prodResp)

	//响应
	ctrl.Response(ctx, prodResp.Code, prodResp.Data)
}

/**
 * 删除商品
 */
func (ctrl *ProductController) DeleteProduct(ctx *gin.Context) {
	var prodReq product.Product
	idStr := ctx.DefaultQuery("id", "0")
	id, _ := strconv.Atoi(idStr)
	prodReq.ProdId = int32(id)
	//ID是必须要传的
	if prodReq.ProdId <= 0 {
		ctrl.ResponseFailed(ctx, 4000, "参数不正确")
		return
	}

	prodRpcClient := middleware.GetProductRpcClient()
	prodResp, _ := prodRpcClient.DeleteProduct(context.Background(), &prodReq)
	log.Info("prodService.DeleteProduct:", prodResp)
	ctrl.Response(ctx, prodResp.Code, prodResp.Data)
}
